package com.dzsw.netty.websocket;

import com.dzsw.netty.websocket.protocal.CProtocol;
import com.dzsw.netty.websocket.util.ProtobufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理前端的登录逻辑,JWT认证成功后更新在线状态并拉取离线消息
 */
@Slf4j
@ChannelHandler.Sharable
public class HeartbeatHandler extends SimpleChannelInboundHandler<CProtocol.Heartbeat> {

    public final static HeartbeatHandler INSTANCE = new HeartbeatHandler();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CProtocol.Heartbeat msg) {
        CProtocol.Heartbeat hb = CProtocol.Heartbeat.newBuilder().build();
        CProtocol.Message message = CProtocol.Message.newBuilder()
                .setHeartbeat(hb)
                .setType(CProtocol.EnumDataType.HEARTBEAT)
                .build();
        log.info("HeartbeatHandler -> channelRead0 ======================== ");
        ProtobufUtil.sendBinaryMessage(ctx, message.toByteArray(), true);
    }

}
